import { Button, message, Modal } from 'antd';
import { useEffect, useState } from 'react';
import ProList from '@ant-design/pro-list';
import {
  addTargetFl,
  delTargetFl,
  editTargetFl,
  getTargetFl,
  jssc,
  sjhq,
} from '@/services';
import { history } from 'umi';

export default () => {
  const [dataSource, setDataSource] = useState<any[]>();
  useEffect(() => {
    getData();
  }, []);
  const getData = async () => {
    const data = await sjhq();
    if (data.msg === '失败') {
      message.error(data.content);
      setDataSource([]);
    } else {
      setDataSource(data.content);
    }
  };

  const del = async (time: string) => {
    Modal.confirm({
      title: '确认反登账？当前年度的计算数据将被删除，且不可恢复。',
      onOk: async () => {
        const data = await jssc({ time });
        if (data.msg === '失败') {
          message.error(data.content);
        } else {
          message.success('登账成功');
          getData();
        }
      },
    });
  };

  return (
    <div style={{ display: 'flex', height: '100%' }}>
      <div
        style={{
          width: '100%',
          height: '100%',
          marginRight: '8px',
          background: '#fff',
        }}
      >
        <ProList<any>
          defaultSize="small"
          rowKey="id"
          headerTitle="会计期间"
          dataSource={dataSource}
          showActions="always"
          key="id"
          onRow={(record) => {
            return {
              onClick: () => {
                history.push(`/calculate/${record.time}`);
              },
            };
          }}
          metas={{
            title: {
              dataIndex: 'time',
              render: (_, record: any) => (
                <p>
                  {record.time}
                  {record.suan === 'false' && '（未登账）'}
                </p>
              ),
            },
            actions: {
              render: (_: any, record: any) =>
                record.suan === 'true' && (
                  <a onClick={() => del(record.time)}>反登账</a>
                ),
            },
          }}
        />
      </div>
    </div>
  );
};
